<script setup lang="ts">
import { useIsvList } from "./index";
const {
  actionButtons,
  dlgShow,
  dlgTitle,
  editFormColumns,
  editFormData,
  editFormRules,
  handleAdd,
  handlePaginationChange,
  handleSave,
  handleSearch,
  pageInfo,
  searchFormColumns,
  searchFormData,
  tableColumns,
  tableData,
  total
} = useIsvList();
import {
  dlgKeysShow,
  showKeysFormColumns,
  showKeysFormData,
  downloadIsvKey,
  viewKeys
} from "./showKeys";
import {
  dlgKeysSetting,
  handleUpdateKeys,
  resetIsvKeys,
  resetPlatformKeys,
  restIsvKeysLoading,
  resetPlatformKeysLoading,
  settingKeysFormColumns,
  settingKeysFormData,
  settingKeysFormRules
} from "@/views/isv/list/isvKeys";
import {
  dlgGroupSetting,
  groupColumns,
  handleUpdateGroup,
  settingGroupFormData
} from "@/views/isv/list/isvGroup";
defineOptions({
  name: "IsvList"
});
</script>
<template>
  <el-card shadow="never">
    <template #header>
      <PlusSearch
        v-model="searchFormData"
        :columns="searchFormColumns"
        :show-number="2"
        label-position="right"
        :has-reset="false"
        @search="handleSearch"
      />
    </template>
    <PlusTable
      :columns="tableColumns"
      :table-data="tableData"
      :action-bar="{ buttons: actionButtons, width: 230, showNumber: 4 }"
      :pagination="{
        total,
        modelValue: pageInfo,
        pageSizeList: [10, 20, 50, 100],
        align: 'right'
      }"
      showOverflowTooltip
      @paginationChange="handlePaginationChange"
    >
      <template #title>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </template>
      <template #plus-cell-keys="scoped">
        <el-link
          v-if="scoped.row.hasKeys"
          type="primary"
          @click="viewKeys(scoped.row)"
        >
          查看
        </el-link>
        <span v-else>未设置</span>
      </template>
    </PlusTable>
    <PlusDialogForm
      v-model:visible="dlgShow"
      v-model="editFormData"
      label-width="200px"
      :dialog="{ title: dlgTitle }"
      :form="{
        columns: editFormColumns,
        rules: editFormRules,
        labelWidth: '100px',
        labelPosition: 'right'
      }"
      @confirm="handleSave"
    />
    <!-- 查看秘钥 -->
    <PlusDialogForm
      v-model:visible="dlgKeysShow"
      v-model="showKeysFormData"
      :dialog="{ title: '秘钥 - 标识☆分配给开发者' }"
      :form="{ group: showKeysFormColumns, labelPosition: 'right' }"
      @confirm="downloadIsvKey"
    >
      <template #dialog-footer="{ handleCancel }">
        <el-button type="success" @click="downloadIsvKey">
          下载开发者秘钥
        </el-button>
        <el-button type="primary" @click="handleCancel">关闭</el-button>
      </template>
    </PlusDialogForm>
    <!-- 设置秘钥 -->
    <PlusDialogForm
      v-model:visible="dlgKeysSetting"
      v-model="settingKeysFormData"
      :dialog="{ title: '秘钥 - 标识☆分配给开发者' }"
      :form="{
        group: settingKeysFormColumns,
        rules: settingKeysFormRules,
        labelPosition: 'right'
      }"
      :hasErrorTip="false"
      @confirm="handleUpdateKeys"
    >
      <template #plus-field-restIsvKeys>
        <el-button
          type="primary"
          :loading="restIsvKeysLoading"
          @click="resetIsvKeys"
        >
          重置ISV秘钥
        </el-button>
      </template>
      <template #plus-field-restPlatformKeys>
        <el-button
          type="primary"
          :loading="resetPlatformKeysLoading"
          @click="resetPlatformKeys"
        >
          重置平台秘钥
        </el-button>
      </template>
    </PlusDialogForm>
    <!-- 设置分组 -->
    <PlusDialogForm
      v-model:visible="dlgGroupSetting"
      v-model="settingGroupFormData"
      :dialog="{ title: '设置分组' }"
      :form="{
        columns: groupColumns,
        labelPosition: 'top'
      }"
      :hasErrorTip="false"
      @confirm="handleUpdateGroup"
    />
  </el-card>
</template>
